<template>
    <div>
<van-address-edit
  :area-list="areaList"
  show-postal
  show-delete
  show-set-default
  show-search-result
  :area-columns-placeholder="['请选择', '请选择', '请选择']"
  @save="onSave"
  @delete="onDelete"
  @change-detail="onChangeDetail"   
  @change-area="getareaCode"
  :address-info ="info"
/>
    </div>
</template>

<script>
import { ref } from 'vue';
import { Toast } from 'vant';
import { areaList } from '@vant/area-data';
import {  fetcheditaddress , fetchdeleteddress } from '@/API/goods.js'
export default {
    props:{
        addressInfo:{
            typeof:String,
              required: true,
        }
    }
    ,
  setup(props) {
    const dizhi =ref('')
    const areaCode=ref('')

    const onChangeDetail = (val) => {
    console.log(val);

    };
    console.log(props.addressInfo);
    const info = JSON.parse(props.addressInfo)
    areaCode.value =  info.areaCode
    info.areaCode= info.areaCode.split("-")[2]
    console.log(info);

   return {
      areaList,
      areaCode,
      onChangeDetail,
      dizhi,
      info
    };
  },
  methods:{
    async onDelete(item){
      let result = await  fetchdeleteddress(item.id)
      Toast(result.message)
      this.$router.back()
    },
     async  onSave(addressInfo) {
        addressInfo.isDefault =  addressInfo.isDefault ? 1 : 0;
        addressInfo.country = addressInfo.county;
       this.$emit('change-area')
        let formData = {
        
        ...addressInfo,
        areaCode:this.areaCode,
        }
        console.log('添加的数据',formData);
        let result = await fetcheditaddress(this.info.id,formData)
        console.log(result);
        if(result.status ==1){
          Toast('修改失败',result.message)
       
        }else{
      
            Toast('修改成功',result.message)
               this.$router.back()
               
        }
       },
       getareaCode(values){
          console.log(JSON.stringify( values));
              this.areaCode = values.map(item => item.code).join('-')
                let user_id = this.$store.state;
              console.log( this.areaCode ,user_id);
       }
  }
};
</script>

<style lang="scss" scoped>

</style>